package com.seh.contentcenter.repository;

import com.seh.contentcenter.es.ESGoods;
import org.springframework.data.elasticsearch.annotations.Highlight;
import org.springframework.data.elasticsearch.annotations.HighlightField;
import org.springframework.data.elasticsearch.annotations.Query;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository("esGoodsRepository")
public interface ESGoodsRepository extends ElasticsearchRepository<ESGoods, String> {
    List<ESGoods> findByGoodsName(String goodsName);

    /**
     * 这段代码通过在es库中搜寻goodsName和goodsCategory字段
     * 进行商品匹配
     * @param keyword
     * @return
     */
    @Highlight(fields = {
            @HighlightField(name = "goodsName"),
            @HighlightField(name = "goodsCategory")
    })
//    @Query("{\"match\":{\"goodsName\":\"?0\"}}")
//    @Query("{\"multi_match\":{\"query\":\"?0\",\"fields\":[\"goodsName\", \"goodsCategory\"]}}")
    @Query("{\"bool\":{\"must\":[{\"multi_match\":{\"query\":\"?0\",\"fields\":[\"goodsName\",\"goodsCategory\"]}},{\"term\":{\"goodsStatus\":0}}]}}")
    SearchHits<ESGoods> find(String keyword);
}
